﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CafeOnline.MvcFramework.Extensions;

namespace CafeOnline.MvcFramework.Grid {
    public class GridOptions {

        #region Types

        public class DynamicViewBag : DynamicObject {

            private readonly Dictionary<string, object> m_dictionary;

            public DynamicViewBag() {
                m_dictionary = new Dictionary<string, object>();
            }

            public override IEnumerable<string> GetDynamicMemberNames() {
                return m_dictionary.Keys;
            }

            public override bool TryGetMember(GetMemberBinder binder, out object result) {
                m_dictionary.TryGetValue(binder.Name, out result);
                return true;
            }

            public override bool TrySetMember(SetMemberBinder binder, object value) {
                m_dictionary[binder.Name] = value;
                return true;
            }
        }

        #endregion Types

        private Dictionary<string, List<Action<Column>>> m_columnFormattings;

        public dynamic ViewBag {
            get;
            private set;
        }

        /// <summary>
        /// Gets or set the caption of the grid.
        /// </summary>
        public string Caption {
            get;
            set;
        }

        public bool NoSubGrid {
            get;
            set;
        }

        public bool Searchable {
            get;
            set;
        }
        public bool Sortable {
            get;
            set;
        }

        public bool Toolbar {
            get;
            set;
        }

        public bool ColumnSelector {
            get;
            set;
        }

        public bool MultiSelect {
            get;
            set;
        }

        public bool HeaderTitles {
            get;
            set;
        }

        public bool HideColumnHeaders {
            get;
            set;
        }

        public bool SearchOnEnter {
            get;
            set;
        }

        public bool EditButton {
            get;
            set;
        }

        public bool DeleteButton {
            get;
            set;
        }

        public string OnSelect { get; set; }

        public bool FooterData {
            get;
            set;
        }

        public string Ctx { get; set; }

        public bool Pager {
            get;
            set;
        }

        public int RowNumber { get; set; }

        public bool Scroll {
            get;
            set;
        }

        public Action<CallGrid> AdditionalSettings {
            get;
            set;
        }


        public void AddColumnFormatting(string columnName, Action<Column> action) {
            List<Action<Column>> columnFormatting;
            if (!m_columnFormattings.TryGetValue(columnName, out columnFormatting)) {
                columnFormatting = new List<Action<Column>>();
                m_columnFormattings.Add(columnName, columnFormatting);
            }
            columnFormatting.Add(action);
        }

        internal void ConfigureColumns(IEnumerable<Column> cols) {
            cols.ForEach(col => {
                List<Action<Column>> columnFormatting;
                if (m_columnFormattings.TryGetValue(col.ColumnName, out columnFormatting)) {
                    columnFormatting.ForEach(cF => cF(col));
                }
                if (InitialSettings != null) {
                    InitialSettings.Where(r => r.field == col.Index).ForEach(r => {
                        //col.SetSearchType(Searchtype.text);
                        col.SetSearchInit(r.data);
                    });
                }
                if (!Sortable) {
                    col.SetSortable(false);
                }
            });
        }

        public Rule[] InitialSettings {
            get;
            set;
        }

        public GridOptions() {
            NoSubGrid = false;
            Searchable = true;
            Toolbar = true;
            MultiSelect = false;
            HeaderTitles = true;
            HideColumnHeaders = false;
            Sortable = true;
            SearchOnEnter = true;
            EditButton = false;
            DeleteButton = false;
            FooterData = false;
            Pager = true;
            RowNumber = 10;
            ColumnSelector = true;
            Scroll = true;
            m_columnFormattings = new Dictionary<string, List<Action<Column>>>();
            ViewBag = new DynamicViewBag();
        }
    }
}
